package com.lh.项目中存在的编码问题.java中的io案例;


import java.io.UnsupportedEncodingException;

/**
 * 作者：idea
 * 日期：2018/7/17
 * 描述：java里面的编解码问题
 */
public class Encode {

    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv+" ");
        }
        return stringBuilder.toString();
    }


    //通过这个函数可以看到不同的格式编码转换出来的结果不一样
    public static void encode() throws UnsupportedEncodingException {
        String name="I am 君山";
        System.out.println(bytesToHexString(name.getBytes("UTF-8")));
        System.out.println(bytesToHexString(name.getBytes("GB2312")));
        System.out.println(bytesToHexString(name.getBytes("GBK")));
        System.out.println(bytesToHexString(name.getBytes("UTF-16")));
    }


    public static void main(String[] args) throws UnsupportedEncodingException {
        encode();
    }
}
